package org.jeecg.modules.SellsSavesModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ParameterModule.entity.LocationInfo;
import org.jeecg.modules.ParameterModule.entity.ReservoirInfo;
import org.jeecg.modules.ParameterModule.entity.ShelvesInfo;
import org.jeecg.modules.ParameterModule.entity.WarehouseInfo;
import org.jeecg.modules.SellsSavesModule.entity.*;
import org.jeecg.modules.SellsSavesModule.mapper.InventoryMapper;
import org.jeecg.modules.SellsSavesModule.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, InstorageDanInfo> implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 入库单的分页查询
     */
    @Override
    public IPage<InstorageDanInfo> query_InstorageDanInfoPageList(Page<InstorageDanInfo> page, String queryCode, String queryStatus) {
        IPage<InstorageDanInfo> pagelist = inventoryMapper.query_InstorageDanInfoPageList(page,queryCode,queryStatus);
        if(pagelist.getRecords().size()>0){
            for (int i=0;i<pagelist.getRecords().size();i++){
                List<InstorageDanGoods> instorageDanGoodsList = inventoryMapper.count_instorageDan_goods(pagelist.getRecords().get(i).getId());
                pagelist.getRecords().get(i).setSumCount(instorageDanGoodsList.size());
                double money = 0;
                if(instorageDanGoodsList.size()>0){
                    for (int k=0;k<instorageDanGoodsList.size();k++){
                        money+=Double.parseDouble(instorageDanGoodsList.get(k).getIntoMoney());
                    }
                    pagelist.getRecords().get(i).setSumMoney(money+"");
                }else {
                    pagelist.getRecords().get(i).setSumMoney("0.00");
                }
            }
        }
        return pagelist;
    }
    /**
     * 根据id查询入库单详情
     */
    @Override
    public InstorageDanInfo query_instorageDanById(String id) {
        InstorageDanInfo datainfo = inventoryMapper.query_instorageDanById(id);
        return datainfo;
    }

    /**
     * 增加入库单
     */
    @Override
    public int add_InstorageDan(InstorageDanInfo datainfo) {
        try {
            inventoryMapper.add_InstorageDan(datainfo);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 修改入库单
     */
    @Override
    public int update_InstorageDan(InstorageDanInfo datainfo) {
        try {
            inventoryMapper.update_InstorageDan(datainfo);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 修改入库单的状态
     */
    @Override
    public int change_InstorageDan_status(String id, String status) {
        try {
            inventoryMapper.change_InstorageDan_status(id,status);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }

    /**
     * 增加入库单下的商品列表
     */
    @Override
    public int add_InstorageDan_goods(InstorageDanGoods datainfo) {
        try {
            inventoryMapper.add_InstorageDan_goods(datainfo);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 入库单下的商品的分页查询
     */
    @Override
    public IPage<InstorageDanGoods> query_InstorageDanGoodsPageList(Page<InstorageDanGoods> page, String top_id) {
        IPage<InstorageDanGoods> pagelist = inventoryMapper.query_InstorageDanGoodsPageList(page,top_id);
        return pagelist;
    }
    /**
     * 删除入库单下的商品列表
     */
    @Override
    public int delete_InstorageDan_goods(String id, String status) {
        try {
            inventoryMapper.delete_InstorageDan_goods(id,status);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 分页查询商品面料统合表
     * @return
     */
    @Override
    public IPage<GoodsInfo> query_GoodsFabricPageList(Page<GoodsInfo> page, String goods_code, String supplierGoodsCode) {
        IPage<GoodsInfo> pagelist = inventoryMapper.query_GoodsFabricPageList(page,goods_code,supplierGoodsCode);
        return pagelist;
    }
    /**
     * 分页查询入库配置项
     * @return
     */
    @Override
    public IPage<InstorageItemsInfo> query_rukuitemsPageList(Page<InstorageItemsInfo> page, String top_id,int type) {
        IPage<InstorageItemsInfo> pagelist = inventoryMapper.query_rukuitemsPageList(page,top_id,type);
        return pagelist;
    }
    /**
     * 入库下商品id查询所有配置的入库项
     */
    @Override
    public List<InstorageItemsInfo> query_rukuitems(String instorageGoodsId) {
        List<InstorageItemsInfo> datalist = inventoryMapper.query_rukuitems(instorageGoodsId);
        return datalist;
    }

    /**
     * 增加入库配置项
     */
    @Override
    public int add_Instorageitems(InstorageItemsInfo itemsInfo) {
        try {
            inventoryMapper.add_Instorageitems(itemsInfo);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 删除入库配置项
     */
    @Override
    public int delete_Instorageitems(String id, String status) {
        try {
            inventoryMapper.delete_Instorageitems(id,status);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 根据id查询入库单下商品详情
     */
    @Override
    public InstorageDanGoods query_instorageDan_goodsById(String id) {
        InstorageDanGoods datainfo = inventoryMapper.query_instorageDan_goodsById(id);
        return datainfo;
    }
    /**
     * 根据入库下的商品id修改剩余库存
     */
    @Override
    public int update_instoragegood_count(String id, String remainingNumber) {
        try {
            inventoryMapper.update_instoragegood_count(id,remainingNumber);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 根据商品id查询所有规格
     */
    @Override
    public List<GoodsSpecifications> query_allSpecificationsBygoodId(String goodsId) {
        List<GoodsSpecifications> datalist = inventoryMapper.query_allSpecificationsBygoodId(goodsId);
        return datalist;
    }
    /**
     * 根据商品id查询所有尺码
     */
    @Override
    public List<GoodsSizeInfo> query_allSizeBygoodId(String goodsId) {
        List<GoodsSizeInfo> datalist = inventoryMapper.query_allSizeBygoodId(goodsId);
        return datalist;
    }
    /**
     * 查询所有仓库
     */
    @Override
    public List<WarehouseInfo> query_allWarehouse() {
        List<WarehouseInfo> datalist = inventoryMapper.query_allWarehouse();
        return datalist;
    }
    /**
     * 查询所有仓库下的库区
     */
    @Override
    public List<ReservoirInfo> query_allReservoirBycangkuId(String cankgu_id) {
        List<ReservoirInfo> datalist = inventoryMapper.query_allReservoirBycangkuId(cankgu_id);
        return datalist;
    }
    /**
     * 根据库区id查询所有货架
     */
    @Override
    public List<ShelvesInfo> query_allShelvesBykuquId(String kuqu_id) {
        List<ShelvesInfo> datalist = inventoryMapper.query_allShelvesBykuquId(kuqu_id);
        return datalist;
    }
    /**
     * 根据货架id查询所有库位
     */
    @Override
    public List<LocationInfo> query_allLocationByhuojiaId(String huojia_id) {
        List<LocationInfo> datalist = inventoryMapper.query_allLocationByhuojiaId(huojia_id);
        return datalist;
    }
    /**
     * 根据入库单id和商品id查询入库单下的商品
     */
    @Override
    public InstorageDanGoods query_instorageDangoodsBygoodId(String instorageDan_id, String good_id) {
        InstorageDanGoods datainfo = inventoryMapper.query_instorageDangoodsBygoodId(instorageDan_id,good_id);
        return datainfo;
    }
    /**
     * 根据id查询入库项详情
     */
    @Override
    public InstorageItemsInfo query_InstorageItemsInfoById(String id) {
        InstorageItemsInfo datainfo = inventoryMapper.query_InstorageItemsInfoById(id);
        return datainfo;
    }

    @Override
    public List<InstorageItemsInfo> query_allInstorageItemsByrukudanId(String rukudan_id, int type) {
        List<InstorageItemsInfo> datalist = inventoryMapper.query_allInstorageItemsByrukudanId(rukudan_id,type);
        return datalist;
    }

    @Override
    public int count_waitRuku(String rukudan_id, int status) {
        return inventoryMapper.count_waitRuku(rukudan_id,status);
    }

    @Override
    public int insert_inventory(InventoryInfo object) {
        try {
            inventoryMapper.insert_inventory(object);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }

    @Override
    public int update_inventory(InventoryInfo object) {
        try {
            inventoryMapper.update_inventory(object);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }

    @Override
    public int insert_inventory_batch(InventoryBatchInfo object) {
        try {
            inventoryMapper.insert_inventory_batch(object);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }

    @Override
    public int update_inventory_batch(InventoryBatchInfo object) {
        try {
            inventoryMapper.update_inventory_batch(object);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }

    @Override
    public InventoryInfo query_InventoryInfoBygoodsId(String goods_id) {
        InventoryInfo datainfo = inventoryMapper.query_InventoryInfoBygoodsId(goods_id);
        return datainfo;
    }

    @Override
    public InventoryBatchInfo query_InventoryBatchBygoodsId(String goods_id, String batch_name) {
        InventoryBatchInfo datainfo = inventoryMapper.query_InventoryBatchBygoodsId(goods_id,batch_name);
        return datainfo;
    }

    @Override
    public GoodsInfo query_goodsDetailBygoodsId(String goods_id) {
        GoodsInfo datainfo = inventoryMapper.query_goodsDetailBygoodsId(goods_id);
        return datainfo;
    }

    /**
     * 修改入库单状态
     */
    @Override
    public int update_rukudan_status(String rukudan_id, int status) {
        try {
            inventoryMapper.update_rukudan_status(rukudan_id,status);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 根据库位id查询入库项详情
     */
    @Override
    public InstorageItemsInfo query_InstorageItemsBylocationId(String locationId) {
        InstorageItemsInfo datainfo = inventoryMapper.query_InstorageItemsBylocationId(locationId);
        return datainfo;
    }

}
